package com.ry.est.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.ry.est.constants.Constants;
import com.ry.est.constants.MaintainStatusEnum;
import com.ry.est.constants.ResultEnum;
import com.ry.est.dao.MaintainOrderDao;
import com.ry.est.dto.EvaluateInfoDTO;
import com.ry.est.dto.MaintainOrderDTO;
import com.ry.est.dto.SimpleOrderDTO;
import com.ry.est.entity.Appointment;
import com.ry.est.entity.MaintainOrder;
import com.ry.est.entity.MaintainOrderFlow;
import com.ry.est.entity.Users;
import com.ry.est.exception.BussinessException;
import com.ry.est.req.SubmitOrderRequest;
import com.ry.est.service.AppointmentService;
import com.ry.est.service.EvaluateService;
import com.ry.est.service.MaintainOrderFlowService;
import com.ry.est.service.MaintainOrderService;
import com.ry.est.util.BeanUtils;
import io.netty.util.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.Date;

/**
 * @author ry
 * @since 2021-11-10 09:35
 **/
@Service
public class MaintainOrderServiceImpl implements MaintainOrderService {
    @Autowired
    private MaintainOrderDao maintainOrderDao;
    @Autowired
    private AppointmentService appointmentService;
    @Autowired
    private EvaluateService evaluateService;
    @Autowired
    private MaintainOrderFlowService maintainOrderFlowService;

    /**
     * 用户提交订单
     * @param users
     * @param orderRequest
     * @return
     */
    @Override
    public SimpleOrderDTO submitOrder(Users users, SubmitOrderRequest orderRequest) {
//        1、校验参数信息。
        Long evaluateId = orderRequest.getEvaluateId();
        EvaluateInfoDTO evaluateInfoDTO = evaluateService.getEvaluateInfoById(evaluateId);
        if (ObjectUtils.isEmpty(evaluateInfoDTO)){
            throw new BussinessException(ResultEnum.FAIL_PARAM);
        }
//        2、保存预约信息。
        saveAppointment(users,orderRequest);
//        3、保存维修订单信息
        MaintainOrder maintainOrder = saveMaintainOrder(users,evaluateInfoDTO);
//        （订单编号yyyyMMddHHmmss+4位随机数+10位订单id信息）。
        //更新订单编号
        String orderNo = generateOrderNo(maintainOrder);
        maintainOrder.setOrderNo(orderNo);
        maintainOrderDao.updateOrderNo(maintainOrder.getId(),orderNo);

//        4、保存维修订单流水信息。
        saveMaintainOrderFLow(users,maintainOrder);
//        5、返回订单编号
        SimpleOrderDTO simpleOrderDTO = new SimpleOrderDTO();
        simpleOrderDTO.setOrderNo(orderNo);
        return simpleOrderDTO;
    }

    @Override
    public MaintainOrderDTO getByOrderNo(String orderNo) {
        MaintainOrder maintainOrder = maintainOrderDao.selectByOrderNo(orderNo, Constants.EST_NOT);

        return BeanUtils.convertToDTO(maintainOrder,MaintainOrderDTO.class);
    }

    @Override
    public int updateOrderStatusToPayed(String orderNo) {
        MaintainOrder maintainOrder = new MaintainOrder();
        maintainOrder.setOrderNo(orderNo);

        return maintainOrderDao.updateOrderStatusByOrderNo(orderNo,Constants.Order.MaintainStatus.PAY_DEPOSIT);
    }

    private void saveMaintainOrderFLow(Users users, MaintainOrder maintainOrder) {
        MaintainOrderFlow maintainOrderFlow = new MaintainOrderFlow();
        maintainOrderFlow.setOrderNo(maintainOrder.getOrderNo());
        //根据订单状态获取对应的枚举
        MaintainStatusEnum maintainStatusEnum = MaintainStatusEnum.getByCode(maintainOrder.getStatus());
        maintainOrderFlow.setOrderStatus(maintainStatusEnum.getCode());
        maintainOrderFlow.setOrderStatusDesc(maintainStatusEnum.getMsg());
        maintainOrderFlow.setOperationUId(users.getId());
        maintainOrderFlow.setOperationName(users.getName());
        maintainOrderFlow.setCreatedUserId(users.getId());
        maintainOrderFlow.setCreatedTime(new Date());

        maintainOrderFlowService.saveMaintainOrderFlow(maintainOrderFlow);
    }

    private String generateOrderNo(MaintainOrder maintainOrder) {
        StringBuilder builder = new StringBuilder();
        String yyyyMMddHHmmss = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        builder.append(yyyyMMddHHmmss);
        String randomNumbers = RandomUtil.randomNumbers(4);
        builder.append(randomNumbers);
        Long id = maintainOrder.getId();
        String s = StrUtil.padPre(String.valueOf(id), 10, '0');
        builder.append(s);

        return builder.toString();
    }


    private MaintainOrder saveMaintainOrder(Users users, EvaluateInfoDTO evaluateInfoDTO) {
        MaintainOrder maintainOrder = new MaintainOrder();
//        maintainOrder.setOrderNo();
        maintainOrder.setUserId(users.getId());
        maintainOrder.setModelId(evaluateInfoDTO.getModelDetailDTO().getId());
        maintainOrder.setEvaluateId(evaluateInfoDTO.getId());
        maintainOrder.setSubscription(evaluateInfoDTO.getSubscription());
        maintainOrder.setPrice(evaluateInfoDTO.getPrice());
        maintainOrder.setStatus(Constants.Order.MaintainStatus.APPOINT);
        maintainOrder.setCreatedUserId(users.getId());
        maintainOrder.setCreatedTime(new Date());
        maintainOrder.setIsDelete(Constants.EST_NOT);

        maintainOrderDao.insert(maintainOrder);

        return maintainOrder;
    }

    private void saveAppointment(Users users, SubmitOrderRequest orderRequest) {
        Appointment appointment = new Appointment();
        appointment.setUserId(users.getId());
        appointment.setPhone(orderRequest.getPhone());
        appointment.setAppintDate(orderRequest.getAppintDate());
        appointment.setTemporalInterval(orderRequest.getTemporalInterval());
        appointment.setAdress(orderRequest.getAdress());
        appointment.setStatus(Constants.Order.AppointStatus.APPOINT);
        appointment.setCreatedUserId(users.getId());

        appointmentService.addAppointment(appointment);
    }
}
